<?php
error_reporting(0);
function_exists('date_default_timezone_set') && date_default_timezone_set('Etc/GMT+8');
define('D_P',__FILE__ ? substr(__FILE__,0,-6) : './');
define('R_P',D_P);
$timestamp=time();

require_once(D_P.'data/cache/config.php');
$very['cvtime'] != 0 && $timestamp += $very['cvtime']*60;
if($_GET['admin'])
{
	$very['ckpath']='/';
	$very['ckdomain']='';
}

makeGDImage();

/*
基本函数
*/

function makeGDImage() {
	if (!function_exists('imagecreate')){
		exit('服务器端环境不支持图片函数，请仔细检查GD库支持');
	}

	global $timestamp;

	if (!isset($_GET['size']))
	{
		$x_size=120; //验证图片宽度，单位像素
		$y_size=30; //验证图片高度
		$fontsize = 18; //字体大小
	}
	else
	{
		list($x_size, $y_size, $fontsize) = explode(',', $_GET['size']);
	}

	$fontnum = 4; //验证字符数量

	/* 图片基础 */
	$img = imagecreate($x_size, $y_size);
	$bgcolor = imagecolorallocate($img, 243, 249, 249); //背景颜色
	$bordercolor = imagecolorallocate($img, 150, 150, 150); //边框颜色
	imagefilledrectangle($img, 0, 0, $x_size - 1, $y_size - 1, $bgcolor); //填充背景

	/* 干涉点 */
	$point_num = 100; //干涉点数目
	for ($i = 0;$i<$point_num;$i++){
		$rand_color = imagecolorallocate($img, rand(0, 255), rand(0, 255), rand(0, 255));
		imagesetpixel($img, rand(0, $x_size), rand(0, $y_size), $rand_color);
	}

	/* 干涉线条 */
	$line_num = 10; //设置干扰线条数目
	for ($i = 0; $i<$line_num; $i++){
		$line_color = imagecolorallocate($img, rand(201, 255), rand(201, 255), rand(201, 255)); //设置干扰线条颜色
		imageline($img, rand(0, $x_size), rand(0, $y_size), rand(0, $x_size), rand(0, $y_size), $line_color);
	}

	/* 随机验证码 */
	$chars = "ABCDEFGHIJKLMNPQRSTUVWXYZ123456789"; //字母表
	$chars_len = strlen($chars); //获取字母表长度
	$ttfFile = array('tahoma', 'verdana');
	$key = array_rand($ttfFile, 1);
	$font = D_P . 'require/fonts/' . $ttfFile[$key] . '.ttf'; //设定随机字体
	$cktxt = '';
	for ($i = 0; $i < $fontnum; $i++)
	{
		$num = rand(0, $chars_len-1); //获取一个随机数
		$txt = substr($chars, $num, 1); //获取一个字符
		$angle = rand(-20, 20); //获取一个随机旋转角度
		$fontcolor = imagecolorallocate($img, rand(0, 200), rand(0, 200), rand(0, 200)); //字体颜色
		imagettftext($img, $fontsize, $angle, $i*$x_size/$fontnum + 10, 20, $fontcolor, $font, $txt);
		$cktxt .= $txt;
	}
	$cktxt = strtolower($cktxt);

	Cookie('cknum', StrCode($timestamp."\t\t".md5($cktxt.$timestamp)), '600');
	header("Expires: Mon, 26 Jul 1970 05:00:00 GMT");
	header("Cache-Control: no-cache, must-revalidate");
	header("Pragma: no-cache");
	header("Content-type: image/png");
	imagepng($img);
	imagedestroy($img);
	exit;
}

function StrCode($string,$action='ENCODE')
{
	global $very;
	$key	= substr(md5($_SERVER["HTTP_USER_AGENT"].$very['hash']),8,18);
	$string	= $action == 'ENCODE' ? $string : base64_decode($string);
	$len	= strlen($key);
	$code	= '';
	for($i=0; $i<strlen($string); $i++){
		$k		= $i % $len;
		$code  .= $string[$i] ^ $key[$k];
	}
	$code = $action == 'DECODE' ? $code : base64_encode($code);
	return $code;
}
function Cookie($ck_Var,$ck_Value,$ck_Time = 'F'){
	global $timestamp,$very;
	//$ck_Time = $ck_Time == 'F' ? $timestamp + 31536000 : ($ck_Value == '' && $ck_Time == 0 ? $timestamp - 31536000 : $ck_Time);
	$S		 = $_SERVER['SERVER_PORT'] == '443' ? 1:0;
	$very['ckdomain'] = '';
	$very['ckpath'] = '/';
	$ck_Time = $timestamp + 3600;
	setCookie($ck_Var,$ck_Value,$ck_Time,$very['ckpath'],$very['ckdomain'],$S);
}

function GetCookie($Var){
	return $_COOKIE[$Var];
}

function CookiePre(){
	global $very;
	return substr(md5($very['hash']),0,5);
}

function randstr($lenth){
	global $very;
	mt_srand((double)microtime() * 1000000);
	for($i=0;$i<$lenth;$i++){
		$randval.= mt_rand(0,9);
	}	$randval=substr(md5($randval.time().$_SERVER["HTTP_USER_AGENT"].$very['hash']),mt_rand(0,32-$lenth),$lenth);
	return $randval;
}

function num_rand($lenth){
	mt_srand((double)microtime() * 1000000);
	for($i=0;$i<$lenth;$i++){
		$randval.= mt_rand(1,6);
	}
	return $randval;
}
?>